package com.extension;

import android.text.TextUtils;
import android.widget.Toast;

import com.btcTool.BtcUtils;
import com.ethUtil.AddressUtils;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.trxUtil.Base58;
import com.trxUtil.ECKey;
import com.trxUtil.Hex;

import java.util.ArrayList;
import java.util.List;

public class RnTest extends ReactContextBaseJavaModule {
    public RnTest(ReactApplicationContext reactContext) {
        super(reactContext);
    }
    // ReactContextBaseJavaModule要求派生类实现getName方法。这个函数用于返回一个字符串
    // 这个字符串用于在JavaScript端标记这个原生模块
    @Override
    public String getName() {
        return "ToastByAndroid";
    }

    // 获取应用包名
    // 要导出一个方法给JavaScript使用，Java方法需要使用注解@ReactMethod
    @ReactMethod
    public void getPackageName() {
        String name = getReactApplicationContext().getPackageName();
        Toast.makeText(getReactApplicationContext(), name, Toast.LENGTH_LONG).show();
    }

    @ReactMethod
    public void tryPromise(String name, String psw, Promise promise) {
        try {
            if (TextUtils.isEmpty(name) && TextUtils.isEmpty(psw)) {
                promise.reject("0", "user name  or psw is empty");
            }
            WritableMap map = Arguments.createMap();
            map.putString("user_id", "success");
            promise.resolve(map);
        } catch (IllegalViewOperationException e) {
            promise.reject("2", e.getMessage());
        }
    }

    @ReactMethod
    public void getTrxAccount(Promise promise){
        try {
            ECKey ecKey = new ECKey();
            String privateKey = Hex.bytesToHexString(ecKey.getPrivKey().toByteArray());
            String address = Base58.encode58Check(ecKey.getAddress());
            WritableMap map = Arguments.createMap();
            map.putString("address", address);
            map.putString("privateKey", privateKey);
            promise.resolve(map);
        }catch (IllegalViewOperationException e){
            promise.reject("2",e.getMessage());
        }
    }

    @ReactMethod
    public void getEthAccount(Promise promise){
        try{
            WritableMap map = Arguments.createMap();
            byte[] bytes = AddressUtils.getPrivateKey();
            map.putString("address", AddressUtils.getAddress(bytes));
            map.putString("privateKey", AddressUtils.bytesToHex(bytes));
            promise.resolve(map);
        }catch (IllegalViewOperationException e){
            promise.reject("2",e.getMessage());
        }
    }

    @ReactMethod
    public void getBtcAccount(Promise promise){
        try{
            WritableMap map = Arguments.createMap();
            String privkey = BtcUtils.getPrivateKeyStr();
            String address = BtcUtils.getBtcAddress(privkey);
            map.putString("address", address);
            map.putString("privateKey", privkey);
            promise.resolve(map);
        }catch (IllegalViewOperationException e){
            promise.reject("2",e.getMessage());
        }
    }
}


